/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.business.product.manage.repository.service;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.business.product.core.domain.ProductBrand;
import info.peigen.hotpot.business.product.core.repository.entity.ProductBrandEntity;
import info.peigen.hotpot.business.product.manage.convertor.ProductBrandManageConvertor;
import info.peigen.hotpot.business.product.manage.repository.mapper.ProductBrandManageMapper;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import info.peigen.hotpot.component.data.repository.beetlsql.service.AbstractBeetlSqlRepository;
import org.noear.solon.annotation.Component;

/**
 * <b>(ProductBrandManageRepository)</b>
 * 管理后台 "商品品牌" 仓储服务
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2024-05-02
 */
@SuppressWarnings("unused")
@Component
public class ProductBrandManageRepository extends AbstractBeetlSqlRepository<ProductBrandManageMapper, ProductBrandEntity> {
    public ProductBrand active(String brandId) {
        Assert.isTrue(StrUtil.isNotEmpty(brandId), () -> new BusinessException(ResultCode.FAILURE, "激活时[brandId]不得为空!"));

        ProductBrandEntity entity = getMapper().findByBrandId(brandId);

        Assert.notNull(entity, () -> new BusinessException(ResultCode.FAILURE, "激活失败!brandId=" + brandId));

        return ProductBrandManageConvertor.convertToDomain(entity);
    }

    public ProductBrand lock(String brandId) {
        Assert.isTrue(StrUtil.isNotEmpty(brandId), () -> new BusinessException(ResultCode.FAILURE, "加锁时[brandId]不得为空!"));

        ProductBrandEntity entity = getMapper().lockByBrandId(brandId);

        Assert.notNull(entity, () -> new BusinessException(ResultCode.FAILURE, "锁定失败!brandId为空!"));

        return ProductBrandManageConvertor.convertToDomain(entity);
    }

    public void removeByBrandId(String brandId) {
        Assert.isTrue(StrUtil.isNotEmpty(brandId), () -> new BusinessException(ResultCode.FAILURE, "删除时[brandId]不得为空!"));

        getMapper().deleteByBrandId(brandId);
    }

    public void store(ProductBrand domain) {
        Assert.isTrue(ObjectUtil.isNotEmpty(domain), () -> new BusinessException(ResultCode.FAILURE, "持久化时[domain]不得为空!"));

        ProductBrandEntity entity = ProductBrandManageConvertor.convertToEntity(domain);

        saveOrUpdate(entity);
    }
}